#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QMap>
#include <QDebug>

struct Dish
{
    QString name;
    int price;
    int count;
};

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //第一张
    QPixmap pic1("1.jpg");
    pic1 = pic1.scaled(ui->label_13->size());
    ui->label_13->setPixmap(pic1);

    //第二张
    QPixmap pic2("2.jpg");
    pic2 = pic2.scaled(ui->label_15->size());
    ui->label_15->setPixmap(pic2);

    //第三张
    QPixmap pic3("3.jpg");
    pic3 = pic3.scaled(ui->label_17->size());
    ui->label_17->setPixmap(pic3);

    //第四张
    QPixmap pic4("4.jpg");
    pic4 = pic4.scaled(ui->label_9->size());
    ui->label_9->setPixmap(pic4);

    //第五张
    QPixmap pic5("5.jpg");
    pic5 = pic5.scaled(ui->label->size());
    ui->label->setPixmap(pic5);

    //第六张
    QPixmap pic6("6.jpg");
    pic6 = pic6.scaled(ui->label_11->size());
    ui->label_11->setPixmap(pic6);

    //第七张
    QPixmap pic7("7.jpg");
    pic7 = pic7.scaled(ui->label_19->size());
    ui->label_19->setPixmap(pic7);

    //第八张
    QPixmap pic8("8.jpg");
    pic8 = pic8.scaled(ui->label_21->size());
    ui->label_21->setPixmap(pic8);

    //第九张
    QPixmap pic9("9.jpg");
    pic9 = pic9.scaled(ui->label_23->size());
    ui->label_23->setPixmap(pic9);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 统一的菜品映射
QMap<QString, MainWindow::Dish> MainWindow::dishMap;

void MainWindow::on_pushButton_2_clicked()
{
    QWidget *p = this->parentWidget();
    if (p) {
        p->show();
    }
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Background,QBrush(QPixmap("jiemian.jpg")));
    setPalette(pal);
    this->close();
}

void MainWindow::on_pushButton_clicked()
{
    PayWindow *p = new PayWindow(this);
    p->setAttribute(Qt::WA_DeleteOnClose);

    connect(this,SIGNAL(sendSignal(QString,QString)),p,SLOT(getSignal(QString,QString)));
    emit sendSignal(ui->textBrowser->toPlainText(),ui->label_7->text());

    p->show();
    this->hide();

    ui->textBrowser->clear();
    ui->label_7->clear();

    dishMap.clear();

}



// 更新菜品显示
void MainWindow::updateDishDisplay()
{
    // 清空显示区域
    ui->textBrowser->clear();

    // 初始化总价
    int totalPrice = 0;

    // 重新显示所有菜品
    for (auto it = dishMap.begin(); it != dishMap.end(); ++it)
    {
        const Dish& dish = it.value();
        if (dish.count > 0)
        {
            QString displayText = QString("%1 x %2 = %3元").arg(dish.name).arg(dish.count).arg(dish.price * dish.count);
            ui->textBrowser->append(displayText);
            totalPrice += dish.price * dish.count; // 当前菜品总价
        }
    }
    //显示总价格
     ui->label_7->setText(QString("总价: %1元").arg(totalPrice));
}

// 更新菜品数量
void MainWindow::updateDishCount(const QString& name, int change)
{
    // 如果菜品不存在，初始化它
    if (!dishMap.contains(name))
    {
        Dish newDish;
        newDish.name = name;
        newDish.count = 0;

        // 根据菜品名称设置价格
        if (name == "百合排骨") newDish.price = 38;
        else if (name == "干烧鱼") newDish.price = 38;
        else if (name == "果仁焗茄子") newDish.price = 28;
        else if (name == "香辣烤羊腿") newDish.price = 78;
        else if (name == "薯条") newDish.price = 18;
        else if (name == "蒜香鸡翅") newDish.price = 25;
        else if (name == "蒜味虾") newDish.price = 99;
        else if (name == "小炒肉") newDish.price = 35;
        else if (name == "鸭胸配时令") newDish.price = 58;
        else newDish.price = 0;

        dishMap.insert(name, newDish);
    }

    // 更新菜品数量
    Dish& dish = dishMap[name];
    dish.count = qMax(0, dish.count + change);

    // 更新显示
    updateDishDisplay();
}

// 百合排骨-1
void MainWindow::on_pushButton_14_clicked()
{
    updateDishCount("百合排骨", -1); // 传递-1表示减少
}

// 百合排骨+1
void MainWindow::on_pushButton_15_clicked()
{
    updateDishCount("百合排骨", 1); // 传递1表示增加
}

// 干烧鱼-1
void MainWindow::on_pushButton_16_clicked()
{
    updateDishCount("干烧鱼", -1);
}

// 干烧鱼+1
void MainWindow::on_pushButton_17_clicked()
{
    updateDishCount("干烧鱼", 1);
}

// 果仁焗茄子-1
void MainWindow::on_pushButton_18_clicked()
{
    updateDishCount("果仁焗茄子", -1);
}

// 果仁焗茄子+1
void MainWindow::on_pushButton_19_clicked()
{
    updateDishCount("果仁焗茄子", 1);
}

// 香辣烤羊腿-1
void MainWindow::on_pushButton_10_clicked()
{
    updateDishCount("香辣烤羊腿", -1);
}

// 香辣烤羊腿+1
void MainWindow::on_pushButton_11_clicked()
{
    updateDishCount("香辣烤羊腿", 1);
}

// 薯条-1
void MainWindow::on_pushButton_3_clicked()
{
    updateDishCount("薯条", -1);
}

// 薯条+1
void MainWindow::on_pushButton_5_clicked()
{
    updateDishCount("薯条", 1);
}

// 蒜香鸡翅-1
void MainWindow::on_pushButton_12_clicked()
{
    updateDishCount("蒜香鸡翅", -1);
}

// 蒜香鸡翅+1
void MainWindow::on_pushButton_13_clicked()
{
    updateDishCount("蒜香鸡翅", 1);
}

// 蒜味虾-1
void MainWindow::on_pushButton_20_clicked()
{
    updateDishCount("蒜味虾", -1);
}

// 蒜味虾+1
void MainWindow::on_pushButton_21_clicked()
{
    updateDishCount("蒜味虾", 1);
}

// 小炒肉-1
void MainWindow::on_pushButton_22_clicked()
{
    updateDishCount("小炒肉", -1);
}

// 小炒肉+1
void MainWindow::on_pushButton_23_clicked()
{
    updateDishCount("小炒肉", 1);
}

// 鸭胸配时令-1
void MainWindow::on_pushButton_24_clicked()
{
    updateDishCount("鸭胸配时令", -1);
}

// 鸭胸配时令+1
void MainWindow::on_pushButton_25_clicked()
{
    updateDishCount("鸭胸配时令", 1);
}
